﻿using DataModule.Models;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataModule.ViewModels
{
    /// <summary>
    /// 数据采集视图模型
    /// </summary>
    public class DataListViewModel : BindableBase
    {
        /// <summary>
        /// 传感器采集快照集合，绑定到 DataGrid。
        /// </summary>
        public ObservableCollection<SensorSnapshot> Snapshots { get; } = new();

        /// <summary>
        /// 模拟采集命令，绑定到按钮。
        /// </summary>
        public DelegateCommand SimulateCommand { get; }

        public DataListViewModel()
        {
            SimulateCommand = new DelegateCommand(SimulateData);
        }

        /// <summary>
        /// 模拟采集一条传感器数据。
        /// </summary>
        private void SimulateData()
        {
            var snapshot = new SensorSnapshot
            {
                DeviceId = "PLC-001",
                Timestamp = DateTime.Now,
                Temperature = Math.Round(35 + new Random().NextDouble() * 5, 2),
                Pressure = Math.Round(100 + new Random().NextDouble() * 20, 2),
                IsFaulted = new Random().Next(0, 10) < 2 // 20% 概率故障
            };

            Snapshots.Add(snapshot);
        }
    }
}
